package org.jsola.hr.task;

import lombok.extern.slf4j.Slf4j;
import org.jsola.hr.constant.ConfigCodeConstants;
import org.jsola.hr.dao.ICompanyDAO;
import org.jsola.hr.dto.SalaryBillSendAddDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.SalaryBillDO;
import org.jsola.hr.entity.TaskDO;
import org.jsola.hr.provider.IMessageProviderService;
import org.jsola.hr.provider.ISiteConfigProviderService;
import org.jsola.hr.service.ISalaryBillSendService;
import org.jsola.hr.service.ITaskService;
import org.jsola.hr.vo.BillSendInfoVO;
import org.jsola.message.dto.WeiXinDTO;
import org.jsola.message.dto.WxMiNiParam;
import org.jsola.message.dto.WxTemplateParam;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 工资条相关任务
 *
 * @author
 */
@Slf4j
@Component
public class HrSalaryBillTask {

    @Autowired
    private ICompanyDAO companyDAO;

    @Autowired
    private IMessageProviderService messageProviderService;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ISalaryBillSendService salaryBillSendService;

    @Autowired
    private ISiteConfigProviderService siteConfigProviderService;

    /**
     * 异步推送微信模板消息
     * @param openIdList openId集合
     * @param salaryBillSendAddDTO 工资条发放添加参数
     * @Param tokenUser 登录用户
     */
    @Async
    public void sendWeChatTemplateMessage(List<BillSendInfoVO> openIdList, SalaryBillSendAddDTO salaryBillSendAddDTO, TokenUser tokenUser){
        WeiXinDTO weiXinDTO = new WeiXinDTO();
        String templateId = siteConfigProviderService.selectValueByCode(ConfigCodeConstants.CODE_SALARY_BILL_SEND_TEMP_ID, tokenUser.getSiteId());
        String appid = siteConfigProviderService.selectValueByCode(ConfigCodeConstants.CODE_APPID, tokenUser.getSiteId());
        weiXinDTO.setTemplateId(templateId);
        weiXinDTO.setAppId(appid);
        CompanyDO companyDO = companyDAO.selectByPrimaryKey(salaryBillSendAddDTO.getCompanyId());
        /**
         * {{first.DATA}}
         * 企业名称：{{keyword1.DATA}}
         * 计薪月份：{{keyword2.DATA}}
         * {{remark.DATA}}
         */
        // 参数
        List<WxTemplateParam> paramList = new ArrayList<>();
        WxTemplateParam param1 = new WxTemplateParam();
        param1.setName("first");
        param1.setValue("你好，本月工资条已发放");
        paramList.add(param1);
        WxTemplateParam param2 = new WxTemplateParam();
        param2.setName("keyword1");
        param2.setValue(companyDO.getName());
        paramList.add(param2);
        WxTemplateParam param3 = new WxTemplateParam();
        param3.setName("keyword2");
        param3.setValue(salaryBillSendAddDTO.getYear() + "年" + salaryBillSendAddDTO.getMonth() + "月");
        paramList.add(param3);
        WxTemplateParam param4 = new WxTemplateParam();
        param4.setName("remark");
        param4.setValue("请点击查看工资条明细");
        paramList.add(param4);

        weiXinDTO.setTemplateParamList(paramList);
        // 小程序跳转
        WxMiNiParam wxMiNiParam = new WxMiNiParam();
        String miniAppid = siteConfigProviderService.selectValueByCode(ConfigCodeConstants.CODE_MINI_APPID, tokenUser.getSiteId());
        String path = siteConfigProviderService.selectValueByCode(ConfigCodeConstants.CODE_PATH_SALARYBILL, tokenUser.getSiteId());

        wxMiNiParam.setAppid(miniAppid);
        weiXinDTO.setMiniProgram(wxMiNiParam);
        for (BillSendInfoVO billSendInfoVO : openIdList) {
            weiXinDTO.setToUser(billSendInfoVO.getOpenId());
            wxMiNiParam.setPagepath(path + "?id=" + billSendInfoVO.getBillId());
            messageProviderService.batchSendWeChatTempMessage(weiXinDTO, tokenUser);
        }
    }

    /**
     * 工资条到期自动确认
     * @param taskId 任务ID
     * @return
     */
    public void runTask(String taskId) {
        TaskDO task = taskService.selectDOById(taskId, "");
        if (Objects.nonNull(taskId)) {
            log.info("开始执行定时任务-----------:{}", task);
            Example example = new Example(SalaryBillDO.class);

            // 拼装当前用户
            TokenUser tokenUser = new TokenUser();
            tokenUser.setSiteId(task.getSiteId());
            tokenUser.setUserId("system");

            // 修改到期工资条确认状态
            Integer resultCount = salaryBillSendService.updateSalaryBillStatus(task.getSubjectId(), tokenUser);

            // 任务置为已完成
            taskService.taskRunEnd(task.getId(), tokenUser);
            log.info("定时任务task:{}执行结束", task.getId());
        }
    }


}
